const { crawlerGamesInfo, qryStats } = require("../crawler/index");

module.exports = {
  async list(ctx) {
    // console.log("ctx.request.body=", ctx.request.body);
    let result = [];
    let code = 2000;
    let lives = [];
    let notStart = [];
    let err = "";
    try {
      result = await crawlerGamesInfo(ctx.request.body);

      lives = result[0];
      notStart = result[1];
      const pList = [];
      for (let g of lives) {
        pList.push(qryStats(g.dataId));
      }
      let statsRes = await Promise.all(pList);
      for (let i = 0; i < lives.length; i++) {
        lives[i].stats = statsRes[i].data.stats;
        lives[i].incidents = statsRes[i].data.incidents;
      }
    } catch (err) {
      err = err;
      console.log(err);
      result = [];
      code = 5000;
    }
    ctx.body = {
      code: code,
      desc: code === 5000 ? err : "查询成功",
      // data: result,
      lives,
      notStart
    };
  }
};
